<?php
class basket
{
	public $db;
	
	public $products;	
	function __construct()
	{		$this->db = new database();		if(isset($_SESSION['basket'])) {
        	$this->products = $_SESSION['basket'];
        }
	}
	
	function save()
	{
		$_SESSION['basket'] = $this->products;
	}
	
	function addProduct($id, $name, $description, $price, $quantity)
	{
		if($quantity > 0) {
			$this->products[$id]['quantity'] = $this->products[$id]['quantity'] + $quantity;
			$this->products[$id]['name'] = $name;
			$this->products[$id]['description'] = $description;
			$this->products[$id]['price'] = $price;	
			$this->save();
		}
	}		function setQuantity($product_id, $quantity)	{
		if($quantity > 0) {			$this->products[$product_id]['quantity'] = $quantity;				$this->save();
		} else if($quantity == 0) {
			$this->delProduct($product_id);
		}	}
	function delProduct($product_id)	{		unset($this->products[$product_id]);				$this->save();	}
	function getCountProducts()
	{
		if(!isset($this->products)) {
			return 0;
		} else {
			return count($this->products);
		}
	}		function getProducts()	{		return $this->products;	}
	
	function getSumPrice()
	{
		foreach($this->products as $product) {
			$price = $price+($product['quantity']*$product['price']);
		}
		
		return $price;
		
	}
}
?>